using Microsoft.Extensions.DependencyInjection;

namespace DYH.CAD.Extensions.Service;

public static class AppHelper
{
    public static void Init(ServiceCollection serviceCollection)
    {
        serviceCollection.SetUpDependencyInjection()
            .AddAssembly([Assembly.GetAssembly(typeof(AppHelper))])
            .BeginInject();
    }

    public static void SetProvider(IServiceProvider serviceProvider)
    {
        _serviceProvider = serviceProvider;
    }

    private static IServiceProvider _serviceProvider =
        new ServiceCollection().BuildServiceProvider();

    public static T GetService<T>() where T : class => _serviceProvider.GetRequiredService<T>();
    public static object GetService(Type t) => _serviceProvider.GetRequiredService(t);
    public static object? TryGetService(Type t) => _serviceProvider.GetService(t);
}